<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Home\Controller;
use Home\Controller\HomeController;
use Think\Controller;

class TraderController extends HomeController {
    public function __initialize(){
        parent::__initialize();
    }

    //立即购买
    public function nowbuy() {
        if (IS_POST) {
            $goods_num = I('number');   // 数量
            $goods_id = I('goods_id');  //商品ID
            //判断规格是否存在，存在读取属性的价格，不存在读取默认的价格
            $guige1 = I('guige1');
            $guige2 = I('guige2');
            if (empty($guige1)) {
                $guige1 = 0;
            }
            if (empty($guige2)) {
                $guige2 = 0;
            }
            //如果获取不到参数直接跳转等到首页
            if (empty($goods_id) && $goods_id < 1) {
                redirect('Index/index');
                die;
            }

            $this->assign("guige1", $guige1);
            $this->assign("guige2", $guige2);
            $this->assign("goods_id", $goods_id);
            $this->assign("goods_num", $goods_num);
            $this->display();
        } else {
            redirect('Index/index');
            die;
        }
    }

    //提交立即购买
    public function DoNowBuy() {
        if (IS_POST) {
            //生成订单和订单产品
            //根据用户购买不同商家的商品创建相应的订单
            //合并支付时，生成支付的ID，并与订单的ID进行关联
            //order_id 和 pay_sn  订单ID和支付ID
            //订单ID是唯一的，一个订单对应一个订单ID，一个订单可对应一个支付ID（单商品购买或者同商家的购买）属于单品付款，一个支付ID对应多个订单ID（多商家的商品购买）属于合并付款
            //每次的单品支付或者合并支付支付的是动态重新获取的，支付的同时修改相应的支付ID
            //生成订单商品的同时，减少相应的库存
            //生成唯一的支付单号
            $paySN = create_trade_id();
            $order_sn = create_trade_id();

            $goods_id = I('goods_id');       //商品ID
            $goods_num = I('goods_num');     //商品数量
            $guige1 = I('guige1');
            $guige2 = I('guige2');
            if (!empty($goods_id) && !empty($guige2)) {
                $spec_id = $guige1 . "_" . $guige2;
            } else if (!empty($goods_id)) {
                $spec_id = $guige1;
            } else {
                $spec_id = 0;
            }

            $where['is_default'] = "1";
            $where['user_id'] = session('user_id');
            $address = M('address')->where($where)->find();
            if(empty($address)){
                $this->error('请填写您的邮寄地址！！');
            }
            
            
            //根据各属性的ID计算出以下数据：
            $where['goods_id'] = $goods_id;
            $goods = M('goods')->where($where)->find();
            
            //判断是否是统一规格，如果是：取商品自身的图片和价格，否：根据属性表取值
            if (!empty($spec_id)) {
                $map['model'] = $spec_id;
                $map['goods_id'] = $goods_id;
                $price = M('goods_attri_info')->where($map)->getField('price');
                $goods['goods_price'] = $price;
                $wheG1['aid'] = $guige1;
                $wheG1['goods_id'] = $goods_id;
                $G1 = M('goods_attri_class')->where($wheG1)->find();
                $wheG2['aid'] = $guige2;
                $wheG2['goods_id'] = $goods_id;
                $G2 = M('goods_attri_class')->where($wheG2)->find();
                $goods['goods_guige'] = $G1['model'] . ':' . $G1['name'] . '&nbsp;&nbsp;' . $G2['model'] . ':' . $G2['name'];
                //减少商品的库存
                M('goods_attri_info')->where($map)->setDec('stock',$goods_num);
            } else if (!empty($guige1) && empty($guige2)) {
                $map['model'] = $guige1;
                $map['goods_id'] = $goods_id;
                $price = M('goods_attri_info')->where($map)->getField('price');
                $goods['goods_price'] = $price;
                $wheG1['aid'] = $guige1;
                $wheG1['goods_id'] = $goods_id;
                $G1 = M('goods_attri_class')->where($wheG1)->find();
                $goods['goods_guige'] = $G1['model'] . ':' . $G1['name'];
                //减少商品的库存
                M('goods_attri_info')->where($map)->setDec('stock',$goods_num);
            } else {
                $goods['goods_guige'] = "默认";
                //减少商品的库存
                $map['goods_id'] = $goods_id;
                M('goods')->where($map)->setDec('goods_stock',$goods_num);
            }
            
            $youhuiquan = I('YHQstore')[$goods['store_id']];
            $manjian = I('MSstore')[$goods['store_id']];
            $voucher_total = getStoreVoucher($paySN, $youhuiquan, $manjian);
            
            $tddvoucherid = I('tddvoucherid');
            $mansong = I('MSstore')[0];
            //将平台优惠券的使用存入到相应的表里
            $tdd = getTaoduduVoucher($paySN, $tddvoucherid,$mansong);
            
            $order_price = price($goods['goods_price'] * $goods_num);
            //运费价格：
            $yunfei = getTransport($goods['transport_id'], $order_price, $goods_num);
            //订单价格：
            $order_pirce = price($order_price + $yunfei - $voucher_total);
            
            $goods['total_price'] = $order_pirce;
            //生成订单数据列表
            $order_message = I('order_message');
            $payment = I('payment');     //支付方式
            //根据商户的ID进行循环生成订单并将相应的商品增加到订单从表里
            $data['order_sn'] = $order_sn;
            $data['pay_sn'] = $paySN;
            $data['store_id'] = $goods['store_id'];
            $data['store_name'] = $goods['store_name'];
            $data['user_id'] = session('user_id');
            $data['user_name'] = session('user_name');
            $data['add_time'] = NOW_TIME;
            $data['payment_code'] = $payment;
            $data['discount_fee'] = $voucher_total;
            $data['shipping_fee'] = $yunfei;
            $data['goods_amount'] = $order_pirce;
            $data['order_from'] = 1;
            $re = M('order')->add($data);
            //订单生成之后，将相应的商品加入到表里
            if ($re) {

                //订单属性表
                $arr['order_id'] = $re;
                $arr['store_id'] = 0;
                $arr['order_message'] = $order_message;
                $arr['address_id'] = $address['address_id'];
                $arr['reciver_name'] = $address['true_name'];
                $arr['reciver_info'] = serialize($address);
                $arr['reciver_areaid_id'] = $address['area_id'];
                $arr['reciver_city_id'] = $address['city_id'];
                M('orderCommon')->add($arr);
                $this->addBuyGoods($goods, $re, $goods_num);
            } else {
                $this->error('系统异常请稍候再试！');
            }
            switch ($payment) {
                case "weixin":
                    //增加之后跳转到微信支付页面
                    // /Wxpay/JsApipay?id=
                    //$url = U('/Wxpay/JsApipay', array('id' => $data['order_sn']));
                    $url = "/Wxpay/paymentConfirm?key=" . auth_encode($data['pay_sn']);
                    redirect($url);
                    break;
                case "alipay":
                    //增加之后跳转到支付宝支付页面
                    $url = HOME_URL . U('/Pay/index/', array('id' => $data['pay_sn'], 'paytype' => 'ALIPAY'));
                    redirect($url);
                    break;
                case "upcash":
                    //增加之后跳转到银联支付页面
                    $url = HOME_URL . U('/UnionPay/doPay', array('id' => $data['pay_sn']));
                    redirect($url);
                    break;
                default:
                    break;
            }
        } else {
            $this->redirect('/User/index');
        }
    }

    //从购物车购买商品（可多选择商品）
    public function cartBuy() {
        $in = implode(',', I('goods_id'));
        $this->assign("in", $in);
        $this->display();
    }

    //从购物车购买商品（可多选择商品）
    public function reloadCartBuy() {
        if (IS_POST) {
            //$goods_ids = I('goods_id');
            //将购买的商品ID存入到session中，提交订单的时候用
            //session('sel_cartgoods', $goods_ids);
            $in = I('goods_id');
            //购物车商品
            $where['user_id'] = session('user_id');
            $where['goods_id'] = array('in', $in);
            $cart = M('cart')->field('user_id,store_id,store_name')->where($where)->group('store_id')->select();

            foreach ($cart as $key => $value) {
                //获取购物车各个商家的商品
                $map['user_id'] = session('user_id');
                $map['store_id'] = $value['store_id'];
                $cart[$key]['goods'] = M('cart')->where($map)->select();
                //获取商品的运费
                foreach ($cart[$key]['goods'] as $k => $v) {
                    $transport_id = M('goods')->where('goods_id=' . $v['goods_id'])->getField('transport_id');
                    if ($transport_id == 0) {
                        $cart[$key]['yunfei'] += 0;
                    } else {
                        $cart[$key]['yunfei'] += getTransport($transport_id, $v['goods_num'] * $v['goods_price'], $v['goods_num']);
                    }
                    $cart[$key]['goodsPrice'] += $v['goods_num'] * $v['goods_price']; //店铺商品总价

                    if ($cart[$key]['yunfei'] == 0) {
                        $cart[$key]['freight'] = "免运费";
                    } else {
                        $cart[$key]['freight'] = "快递费：" . $cart[$key]['yunfei'] . "元";
                    }
                }
                //获取店铺的满减送优惠信息
                $cart[$key]['storeMS'] = tddMansong($cart[$key]['goodsPrice'], $value['store_id']);
                if (!empty($cart[$key]['storeMS']) || !empty($cart[$key]['yunfei'])) {
                    $cart[$key]['goodsPrice'] = price($cart[$key]['storeMS']['totalPrice'] + $cart[$key]['yunfei']);
                }
                $T += $cart[$key]['goodsPrice'];
            }

            $html .= '<div class="title">商品清单</div>';
            $html .= '<table border="0" cellpadding="0" cellspacing="0" width="100%" class="checkgoods">';
            $html .= '<tbody>';
            $html .= '<tr>';
            $html .= '<th width="50%" align="left" class="tdone">商品</th>';
            $html .= '<th>数量</th>';
            $html .= '<th>单价</th>';
            $html .= '<th>小计</th>';
            $html .= '</tr>';
            $html .= '</tbody>';
            $html .= '</table>';

            foreach ($cart as $key => $value) {

                $html .= '<table border="0" cellpadding="0" cellspacing="0" width="100%" class="checkgoods">';
                $html .= '<tbody>';
                $html .= '<tr>';
                $html .= '<td colspan="5" style="background:#FAFAFA;border-top:1px solid #eee;font-weight:700;padding:5px 10px">' . $value['store_name'] . '</td>';
                $html .= '</tr>';
                foreach ($value['goods'] as $k => $v) {
                    $html .= '<tr>';
                    $html .= '<td width="30%" style="border-top:none">';
                    $html .= '<div class="thumb_name">';
                    $html .= '<dl>';
                    $html .= '<dt>';
                    $html .= '<a href="' . HOME_URL . '/Goods/' . $v['goods_id'] . '" target="_blank">';
                    $html .= '<img src="' . $v['goods_image'] . '!100x100" style="border:1px solid #eee" title="' . $v['goods_title'] . '">';
                    $html .= '</a>';
                    $html .= '</dt>';
                    $html .= '<dd>';
                    $html .= '<a href="' . HOME_URL . '/Goods/' . $v['goods_id'] . '" target="_blank" class="f6">' . $v['goods_title'] . '</a>';
                    $html .= '<br />';
                    $html .= '<input type="hidden" name="goods_id[]" value="' . $v['goods_id'] . '" />';
                    $html .= '<font class="attrname">默认</font>';
                    $html .= '</dd>';
                    $html .= '</dl>';
                    $html .= '</div>';
                    $html .= '</td>';
                    $html .= '<td width="10%" align="center" style="border-top:none">';
                    $html .= '<div class="jm_cartnum">' . $v['goods_num'] . '</div>';
                    $html .= '</td>';
                    $html .= '<td width="10%" align="center" class="price_font" style="border-top:none">￥' . $v['goods_price'] . '</td>';
                    $html .= '<td width="10%" align="center" class="price_font m_parice" style="border-top:none">￥<span>' . price($v['goods_price'] * $v['goods_num']) . '</span></td>';
                    $html .= '</tr>';
                }
                $html .= '<tr>';
                $html .= '<td colspan="2" bgcolor="#ffffff" align="left" class="shipping_type">';
                $html .= '<span id="picktxt916"></span>';
                $html .= '<p class="shipping_desc" id="desc_916">由商家选择合作快递为您配送： ' . $value['freight'] . '</p>';
                $html .= '</td>';
                $html .= '<td colspan="2" bgcolor="#ffffff" align="right" class="shipping_type">';
                $html .= $value['storeMS']['mansong'];
                $html .= '<br />';
                $html .= getStoreVoucher_PC($value['goodsPrice'], $value['store_id']);
                $html .= '<br />店铺合计（含运费）:<font class="main-color" id="goods_zong">￥<b>' . $value['goodsPrice'] . '</b></font>';
                $html .= '<input class="dp_price_val" value="' . $value['goodsPrice'] . '" type="hidden" />';
                $html .= '</td>';
                $html .= '</tr>';
                $html .= '</tbody>';
                $html .= '</table>';
                $html .= '<table border="0" cellpadding="0" cellspacing="0" width="100%" class="checkgoods" style="border-top:0">';
                $html .= '<tbody>';
                $html .= '<tr>';
                $html .= '<td colspan="4" class="tdother2">';
                $html .= '<div class="checkout_other">';
                $html .= '<a class="jmbag" href="javascript:void(0);" onclick="showCheckoutOther(this);"><span>+</span>订单留言</a>';
                $html .= '<table class="subbox_other" width="100%">';
                $html .= '<tbody width="100%" cellpadding="5" cellspacing="0">';
                $html .= '<tr>';
                $html .= '<td align="right" width="100" valign="top">订单留言：</td>';
                $html .= '<td colspan="4"><textarea name="order_message[]" cols="80" rows="3" id="postscript" class="textarea"></textarea></td>';
                $html .= '</tr>';
                $html .= '</tbody>';
                $html .= '</table>';
                $html .= '</div>';
                $html .= '</td>';
                $html .= '</tr>';
                $html .= '</tbody>';
                $html .= '</table>';
                $html .= '<div class="blank10"></div>';
            }

            //获取平台的满减送优惠信息
            $tddMS = tddMansong($T);

            //优惠券信息（平台优惠券）
            $a['voucher'] = getTddVoucher_PC($T);
            $a['html'] = $html;
            $a['mansong'] = $tddMS['mansong'];
            $a['totalPrice'] = price($tddMS['totalPrice']);
            return $this->ajaxReturn($a);
        }
    }

    //提交购物车购买的商品订单（多商家多产品）
    public function DoCartBuy() {
        if (IS_POST) {
            $in = implode(',', I('goods_id'));
            //购物车商品
            $where['user_id'] = session('user_id');
            $where['goods_id'] = array('in', $in);
            $cart = M('cart')->field('user_id,store_id,store_name')->where($where)->group('store_id')->select();
            //根据商家分组创建各个商家的订单
            //生成唯一的支付单号
            //购物车合并商品支付只需要生成一个支付单号即可
            $paySN = create_trade_id();
            $payment = I('payment');     //支付方式
            foreach ($cart as $key => $value) {
                $order_sn = create_trade_id();
                $order_message[$key] = I('order_message')[$key];
                //获取店铺的优惠总额度
                $youhuiquan[$key] = I('YHQstore')[$value['store_id']];
                $manjian[$key] = I('MSstore')[$value['store_id']];
                $voucher_total[$key] = getStoreVoucher($paySN, $youhuiquan[$key], $manjian[$key]);
                //订单总价
                //获取购物车此商家的所有商品
                $map['user_id'] = session('user_id');
                $map['store_id'] = $value['store_id'];
                $map['goods_id'] = array('in', $in);
                $storeGoods = M('cart')->where($map)->select();
                foreach ($storeGoods as $k => $v) {
                    $order_pirce[$key] +=($v['goods_price'] * $v['goods_num']);
                    //商家运费
                    $transport_id[$key] = M('goods')->where('goods_id='.$v['goods_id'])->getField('transport_id');
                    $yunfei[$key] += getTransport($transport_id[$key], $order_pirce[$key], $v['goods_num']);
                }
                //订单实际支付的费用为
                $order_pirce[$key] = $order_pirce[$key] + $yunfei[$key] - $voucher_total[$key];
                //创建订单

                $data['order_sn'] = $order_sn;
                $data['pay_sn'] = $paySN;
                $data['store_id'] = $value['store_id'];
                $data['store_name'] = $value['store_name'];
                $data['user_id'] = session('user_id');
                $data['user_name'] = session('user_name');
                $data['add_time'] = NOW_TIME;
                $data['payment_code'] = $payment;
                $data['discount_fee'] = $voucher_total[$key];
                $data['shipping_fee'] = $yunfei[$key];
                $data['goods_amount'] = $order_pirce[$key];
                $data['order_from'] = 1;
                $re = M('order')->add($data);
                //订单生成之后，将相应的商品加入到表里
                if ($re) {
                    $where['is_default'] = "1";
                    $where['user_id'] = session('user_id');
                    $address = M('address')->where($where)->find();
                    //订单属性表
                    $arr['order_id'] = $re;
                    $arr['store_id'] = $v['store_id'];
                    $arr['order_message'] = $order_message[$key];
                    $arr['address_id'] = $address['address_id'];
                    $arr['reciver_name'] = $address['true_name'];
                    $arr['reciver_info'] = serialize($address);
                    $arr['reciver_areaid_id'] = $address['area_id'];
                    $arr['reciver_city_id'] = $address['city_id'];
                    M('orderCommon')->add($arr);
                    //添加次订单下的所有商品
                    $this->addCartGoods($in, $re, $value['store_id']);
                } else {
                    $this->error('系统异常请稍候再试！');
                }
            }
            $tddvoucherid = I('tddvoucherid');
            $mansong = I('MSstore')[0];
            //将平台优惠券的使用存入到相应的表里
            $tdd = getTaoduduVoucher($paySN, $tddvoucherid,$mansong);
            switch ($payment) {
                case "weixin":
                    //增加之后跳转到微信支付页面
                    // /Wxpay/JsApipay?id=
                    //$url = U('/Wxpay/JsApipay', array('id' => $data['order_sn']));
                    $url = "/Wxpay/paymentConfirm?key=" . auth_encode($data['pay_sn']);
                    redirect($url);
                    break;
                case "alipay":
                    //增加之后跳转到支付宝支付页面
                    $url = HOME_URL . U('/Pay/index/', array('id' => $data['pay_sn'], 'paytype' => 'ALIPAY'));
                    redirect($url);
                    break;
                case "upcash":
                    //增加之后跳转到银联支付页面
                    $url = HOME_URL . U('/UnionPay/doPay', array('id' => $data['pay_sn']));
                    redirect($url);
                    break;
                default:
                    break;
            }
        } else {
            $this->redirect('/User/index');
        }
    }

    //增加从购物车里面购买的商品
    protected function addCartGoods($in, $re, $store_id) {
        //获取购物车此商家的所有商品
        $map['user_id'] = session('user_id');
        $map['store_id'] = $store_id;
        $map['goods_id'] = array('in', $in);
        $storeGoods = M('cart')->where($map)->select();
        foreach ($storeGoods as $k => $v) {
            //判断是否是统一规格，如果是：取商品自身的图片和价格，否：根据属性表取值
            $datas['order_id'] = $re;
            $datas['goods_id'] = $v['goods_id'];
            $datas['goods_title'] = $v['goods_title'];
            $datas['goods_price'] = $v['goods_price'];
            $datas['goods_num'] = $v['goods_num'];
            $datas['goods_image'] = $v['goods_image'];
            $datas['goods_attri_info'] = $v['goods_attri_info'];
            $datas['store_id'] = $v['store_id'];
            $datas['user_id'] = session('user_id');
            $datas['goods_class'] = $v['goods_class'];
            M('orderGoods')->add($datas);
            
            //将用户的购物车商品删除
            $where['goods_id'] = $v['goods_id'];
            M('cart')->where($where)->delete();
            
            //减少商品的库存
            if(!empty($v['goods_attri_model'])){
                $map['model'] = $v['goods_attri_model'];
                M('goods_attri_info')->where($map)->setDec('stock',$v['goods_num']);
            }else{
                $map['goods_id'] = $v['goods_id'];
                M('goods')->where($map)->setDec('goods_stock',$v['goods_num']);
            }
        }
    }

    //增加相应的商品(立即购买下订单)
    protected function addBuyGoods($arr, $order_id, $goods_num) {
        //判断是否是统一规格，如果是：取商品自身的图片和价格，否：根据属性表取值
        $datas['order_id'] = $order_id;
        $datas['goods_id'] = $arr['goods_id'];
        $datas['goods_title'] = $arr['goods_title'];
        $datas['goods_price'] = $arr['goods_price'];
        $datas['goods_num'] = $goods_num;
        $datas['goods_image'] = $arr['goods_thumb'];
        $datas['goods_attri_info'] = $arr['goods_guige'];
        $datas['goods_pay_price'] = $arr['total_price'];
        $datas['store_id'] = $arr['store_id'];
        $datas['user_id'] = session('user_id');
        $datas['goods_class'] = $arr['goods_class'];
        M('orderGoods')->add($datas);
    }

    //商品详情页面加入购物车
    public function addcar() {

        $goods_id = I('goods_id');
        $goods_num = I('num');
        $guige1 = I('guige1');
        $guige2 = I('guige2');
        if (!empty($goods_id) && !empty($guige2)) {
            $spec_id = $guige1 . "_" . $guige2;
        } else if (!empty($goods_id)) {
            $spec_id = $guige1;
        } else {
            $spec_id = 0;
        }

        //判断商品是否已经加入到购物车，如果加入到购物车，直接数量增加1
        $where['goods_id'] = $goods_id;
        $where['user_id'] = session('user_id');
        $where['model'] = $spec_id;
        $count = M('cart')->where($where)->count();

        if ($count <= 0) {
            $map['goods_id'] = $goods_id;
            $goods = M('goods')->where($map)->find();
            if (!empty($spec_id)) {
                $map['model'] = $spec_id;
                $map['goods_id'] = $goods_id;
                $price = M('goods_attri_info')->where($map)->getField('price');
                $goods['goods_price'] = $price;
                $wheG1['aid'] = $guige1;
                $wheG1['goods_id'] = $goods_id;
                $G1 = M('goods_attri_class')->where($wheG1)->find();
                $wheG2['aid'] = $guige2;
                $wheG2['goods_id'] = $goods_id;
                $G2 = M('goods_attri_class')->where($wheG2)->find();
                $goods['goods_guige'] = $G1['model'] . ':' . $G1['name'] . '&nbsp;&nbsp;' . $G2['model'] . ':' . $G2['name'];
            } else if (!empty($guige1) && empty($guige2)) {
                $map['model'] = $guige1;
                $map['goods_id'] = $goods_id;
                $price = M('goods_attri_info')->where($map)->getField('price');
                $goods['goods_price'] = $price;
                $wheG1['aid'] = $guige1;
                $wheG1['goods_id'] = $goods_id;
                $G1 = M('goods_attri_class')->where($wheG1)->find();
                $goods['goods_guige'] = $G1['model'] . ':' . $G1['name'];
            } else {
                $goods['goods_guige'] = "默认";
            }

            $data['user_id'] = session('user_id');
            $data['store_id'] = $goods['store_id'];
            $data['store_name'] = $goods['store_name'];
            $data['goods_id'] = $goods_id;
            $data['goods_title'] = $goods['goods_title'];
            $data['goods_num'] = $goods_num;
            $data['goods_price'] = $goods['goods_price'];
            $data['goods_image'] = $goods['goods_thumb'];
            $data['goods_attri_model'] = $spec_id;
            $data['goods_attri_info'] = $goods['goods_guige'];
            $data['goods_yprice'] = $goods['goods_markprice'];
            $data['goods_class'] = $goods['goods_class'];
            $re = M('cart')->add($data);
        } else {
            $re = M('cart')->where($where)->setInc('goods_num', $goods_num);
        }
        if ($re) {
            $a['p'] = TRUE;
            $a['n'] = M('cart')->where('user_id=' . session('user_id'))->count();
        } else {
            $a['p'] = FALSE;
            $a['n'] = M('cart')->where('user_id=' . session('user_id'))->count();
        }
        $this->ajaxReturn($a);
    }

    //自动加载购物车
    public function reloadCart() {
        $map['user_id'] = session('user_id');
        $list = M('cart')->where($map)->select();
        if (!empty($list)) {
            $html .= '<div class="cart-list">';
            foreach ($list as $key => $value) {
                $total +=$value['goods_price'] * $value['goods_num'];
                $html .= '<div class="cart-item">';
                $html .= '<div class="item-goods">';
                $html .= '<span class="p-img"><a href="' . HOME_URL . '/Goods/' . $value['goods_id'] . '"><img src="' . $value['goods_image'] . '" width="50" height="50" alt=""></a></span>';
                $html .= '<div class="p-name">';
                $html .= '<a href="' . HOME_URL . '/Goods/' . $value['goods_id'] . '" title="' . $value['goods_title'] . '">' . $value['goods_title'] . '</a>';
                $html .= '</div>';
                $html .= '<div class="p-price">单价：<strong>￥' . $value['goods_price'] . '</strong>&nbsp;×' . $value['goods_num'] . '</div>';
                $html .= '<a href="javascript:;" class="p-del" onclick="deleteCartGoods($(this), ' . $value['goods_id'] . ')">删除</a>';
                $html .= '</div>';
                $html .= '</div>';
            }
            $html .= '</div>';

            $html .= '<div class="cart-panel-footer">';
            $html .= '<div class="cart-footer-checkout">';
            $html .= '<div class="number"><strong class="count">' . count($list) . '</strong>件商品</div>';
            $html .= '<div class="sum">共计：<strong class="total">￥：' . $total . '</strong></div>';
            $html .= '<a class="btn" href="/Cart/index.html" target="_blank">去购物车结算</a>';
            $html .= '</div>';
            $html .= '</div>';
        } else {
            $html .= '<div class="tip-box">';
            $html .= '<i class="tip-icon"></i>';
            $html .= '<div class="tip-text">您的购物车里什么都没有哦<br>';
            $html .= '<a class="main-color" href="' . HOME_URL . '">再去看看吧</a>';
            $html .= '</div>';
            $html .= '</div>';
        }
        return $this->ajaxReturn($html);
    }

    //加载购买的商品
    public function reloadGoods() {

        $goods_id = I('goods_id');
        $goods_num = I('goods_num');
        //获取商品的图片及属性
        $map['goods_id'] = $goods_id;
        $goods = M('goods')->where($map)->find();
        //判断规格是否存在，存在读取属性的价格，不存在读取默认的价格
        $guige1 = I('guige1');
        $guige2 = I('guige2');

        if (!empty($guige1) && !empty($guige2)) {
            $map['model'] = $guige1 . "_" . $guige2;
            $map['goods_id'] = $goods_id;
            $price = M('goods_attri_info')->where($map)->getField('price');
            $goods['goods_stock'] = M('goods_attri_info')->where($map)->getField('stock');
            $goodsPrice = price($goods_num * $price);
            $goods['goods_price'] = $price;

            $wheG1['aid'] = $guige1;
            $wheG1['goods_id'] = $goods_id;
            $G1 = M('goods_attri_class')->where($wheG1)->find();
            $wheG2['aid'] = $guige2;
            $wheG2['goods_id'] = $goods_id;
            $G2 = M('goods_attri_class')->where($wheG2)->find();
            $goods['goods_guige'] = $G1['model'] . ':' . $G1['name'] . '&nbsp;&nbsp;' . $G2['model'] . ':' . $G2['name'];
        } else if (!empty($guige1) && empty($guige2)) {
            $map['model'] = $guige1;
            $map['goods_id'] = $goods_id;
            $price = M('goods_attri_info')->where($map)->getField('price');
            $goods['goods_stock'] = M('goods_attri_info')->where($map)->getField('stock');
            $goodsPrice = price($goods_num * $price);
            $goods['goods_price'] = $price;
            $wheG1['aid'] = $guige1;
            $wheG1['goods_id'] = $goods_id;
            $G1 = M('goods_attri_class')->where($wheG1)->find();
            $goods['goods_guige'] = $G1['model'] . ':' . $G1['name'];
        } else {
            $goodsPrice = price($goods_num * $goods['goods_price']);
            $goods['goods_guige'] = "默认";
        }

        //获取商品的运费
        $yunfei = getTransport($goods['transport_id'], $goodsPrice, $goods_num);
        //获取店铺的满减送优惠信息
        $storeMS = tddMansong($goodsPrice, $goods['store_id']);
        if ($yunfei == 0) {
            $freight = "免运费";
        }else if($yunfei >= 99){
            $freight = "此地区不支持配送！！<input id='noExpree_input' type='hidden' />";
            $yunfei = 0;
        }else{
            $freight = "快递费：" . $yunfei . "元";
        }
        if (!empty($storeMS['totalPrice']) || !empty($yunfei)) {
            $goodsPrice = price($storeMS['totalPrice'] + $yunfei);
        }
        //获取平台的满减送优惠信息
        $tddMS = tddMansong($storeMS['totalPrice']);
        if (!empty($tddMS['totalPrice']) || !empty($yunfei)) {
            $totalPrice = price($tddMS['totalPrice'] + $yunfei);
        }
        
        
        
        $html .= '<div class="concent"><div id="payTable"><h3>确认订单信息</h3>';
        $html .= '<div class="cart-table-th"><div class="wp">';
        $html .= '<div class="th th-item"><div class="td-inner">商品信息</div></div>';
        $html .= '<div class="th th-price"><div class="td-inner">单价</div></div>';
        $html .= '<div class="th th-amount"><div class="td-inner">数量</div></div>';
        $html .= '<div class="th th-sum"><div class="td-inner">金额</div></div>';
        $html .= '<div class="th th-oplist"><div class="td-inner">配送方式</div></div>';
        $html .= '</div></div>';
        $html .= '<div class="clear"></div>';
        
        $html .= '<div class="bundle  bundle-last">';
        $html .= '<div class="bundle-main">';
        $html .= '<ul class="item-content clearfix">';
        $html .= '<div class="pay-phone">';
        $html .= '<li class="td td-item"><div class="item-pic"><a href="' . HOME_URL . '/Goods/' . $goods['goods_id'] . '" class="J_MakePoint"><img src="' . $goods['goods_thumb'] . '" class="itempic J_ItemImg"></a></div>';
        $html .= '<div class="item-info"><div class="item-basic-info"><a href="' . HOME_URL . '/Goods/' . $goods['goods_id'] . '" class="item-title J_MakePoint" data-point="tbcart.8.11">' . $goods['goods_title'] . '</a></div></div></li>';
        $html .= '<li class="td td-info"><div class="item-props"><span class="sku-line">' . $goods['goods_guige'] . '颜色：12#川南玛瑙</span><span class="sku-line">包装：裸装</span></div>';
        $html .= '<input type="hidden" name="goods_id" value="' . $goods['goods_id'] . '" /><input type="hidden" name="guige1" value="' . $guige1 . '" /><input type="hidden" name="guige2" value="' . $guige2 . '" /></li>';
        $html .= '<li class="td td-price"><div class="item-price price-promo-promo"><div class="price-content"><em class="J_Price price-now">' . $goods['goods_price'] . '</em></div></div></li></div>';
        $html .= '<li class="td td-amount"><div class="amount-wrapper "><div class="item-amount "><span class="phone-title">购买数量</span>';
        $html .= '<div class="sl">';
        
        $html .= '<span class="min am-btn jian" onclick="goods_as($(this))">-</span>';
        $html .= '<input type="text" stock="' . $goods['goods_stock'] . '" name="goods_num" guige1="' . $guige1 . '" guige2="' . $guige2 . '" goods_id="' . $goods['goods_id'] . '" value="' . $goods_num . '" size="4" class="text_box jminputBg" />';
        $html .= '<span class="jmadd jia add am-btn" onclick="goods_as($(this))">+</span>';
        
        $html .= '</div></div></div></li>';
        $html .= '<li class="td td-sum"><div class="td-inner"><em tabindex="0" class="J_ItemSum number">' . (price($goods['goods_price'] * $goods_num)) . '</em></div></li>';
        $html .= '<li class="td td-oplist"><div class="td-inner"><span class="phone-title">配送方式</span><div class="pay-logis"><b class="sys_item_freprice"> ' . $freight . '</b></div></div></li>';
        $html .= '</ul><div class="clear"></div>';
        $html .= '</div><div class="clear"></div></div></div>';
        
        $html .= '<div class="clear"></div>';
        $html .= '<div class="pay-total">';
        $html .= '<div class="order-extra">';
        $html .= '<div class="order-user-info">';
        $html .= '<div id="holyshit257" class="memo">';
        $html .= '<label>买家留言：</label>';
        $html .= '<input type="text" name="postscript" title="选填,对本次交易的说明（建议填写已经和卖家达成一致的说明）" placeholder="选填,建议填写和卖家达成一致的说明" class="memo-input J_MakePoint c2c-text-default memo-close">';
        $html .= '<div class="msg hidden J-msg">';
        $html .= '<p class="error">最多输入500个字符</p></div></div></div>';
        $html .= '</div>';
        
        $html .= '<div class="buy-agio">';
        $html .= '<li class="td td-coupon">';
        $html .= getStoreVoucher_PC(($goods['goods_price'] * $goods_num), $goods['store_id']);
        $html .= '</li>';
        $html .= '<li class="td td-bonus">' . $storeMS['mansong'].'</li>';
        $html .= '</div><div class="clear"></div></div>';
        
        
        $html .= '<div class="buy-point-discharge ">';
        $html .= '<p class="price g_price ">合计（含运费） <span>¥</span><em class="pay-sum">' . $goodsPrice . '</em></p>';
        $html .= '</div>';


//        $html .= '<td colspan="5" style="background:#FAFAFA;border-top:1px solid #eee;font-weight:700;padding:5px 10px">' . $goods['store_name'] . '</td>';
//        $html .= '</tr>';
//        $html .= '<tr>';
//        $html .= '<td width="30%" style="border-top:none">';
//        $html .= '<div class="thumb_name">';
//        $html .= '<dl>';
//        $html .= '<dt>';
//        $html .= '<a href="' . HOME_URL . '/Goods/' . $goods['goods_id'] . '" target="_blank"><img src="' . $goods['goods_thumb'] . '!100x100" style="border:1px solid #eee" title="' . $goods['goods_title'] . '" /></a>';
//        $html .= '</dt>';
//        $html .= '<dd>';
//        $html .= '<a href="' . HOME_URL . '/Goods/' . $goods['goods_id'] . '" target="_blank" class="f6">' . $goods['goods_title'] . '</a><br>';
//        $html .= '<input type="hidden" name="goods_id" value="' . $goods['goods_id'] . '" /><input type="hidden" name="guige1" value="' . $guige1 . '" /><input type="hidden" name="guige2" value="' . $guige2 . '" />';
//        $html .= '<font class="attrname">' . $goods['goods_guige'] . '</font>';
//        $html .= '</dd>';
//        $html .= '</dl>';
//        $html .= '</div>';
//        $html .= '</td>';
//        $html .= '<td width="10%" align="center" style="border-top:none">';
//        $html .= '<div class="jm_cartnum">';
//        $html .= '<span class=" jian" onclick="goods_as($(this))">-</span>';
//        $html .= '<input type="text" stock="' . $goods['goods_stock'] . '" name="goods_num" guige1="' . $guige1 . '" guige2="' . $guige2 . '" goods_id="' . $goods['goods_id'] . '" value="' . $goods_num . '" size="4" class="jminputBg" />';
//        $html .= '<span class="jmadd jia" onclick="goods_as($(this))">+</span>';
//        $html .= '</div>';
//        $html .= '</td>';
//        $html .= '<td width="10%" align="center" class="price_font" style="border-top:none">￥' . $goods['goods_price'] . '</td>';
//        $html .= '<td width="10%" align="center" class="price_font m_parice" style="border-top:none">￥<span>' . (price($goods['goods_price'] * $goods_num)) . '</span></td>';
//        $html .= '</tr>';
//        $html .= '<tr>';
//        $html .= '<td colspan="2" bgcolor="#ffffff" align="left" class="shipping_type">';
//        $html .= '<span id="picktxt916"></span>';
//        $html .= '<p class="shipping_desc" id="desc_916">由商家选择合作快递为您配送： ' . $freight . '</p>';
//        $html .= '</td>';
//        $html .= '<td colspan="2" bgcolor="#ffffff" align="right" class="shipping_type">' . $storeMS['mansong'] . '<br />';
//        $html .= getStoreVoucher_PC(($goods['goods_price'] * $goods_num), $goods['store_id']) . '<br/>';
//        $html .= '店铺合计（含运费）:<font class="main-color" id="goods_zong">￥<b>' . $goodsPrice . '</b></font><input class="dp_price_val" value="' . $goodsPrice . '" type="hidden" />';
//        $html .= '</td>';
//        $html .= '</tr>';
//        $html .= '</tbody>';
//        $html .= '</table>';
//        $html .= '<table border="0" cellpadding="0" cellspacing="0" width="100%" class="checkgoods" style="border-top:0">';
//        $html .= '<tbody>';
//        $html .= '<tr>';
//        $html .= '<td colspan="4" class="tdother2">';
//        $html .= '<div class="checkout_other">';
//        $html .= '<a class="jmbag" href="javascript:void(0);" onclick="showCheckoutOther(this);"><span>+</span>订单留言</a>';
//        $html .= '<table class="subbox_other" width="100%">';
//        $html .= '<tbody width="100%" cellpadding="5" cellspacing="0">';
//        $html .= ' <tr>';
//        $html .= '<td align="right" width="100" valign="top">订单留言：</td>';
//        $html .= '<td colspan="4">';
//        $html .= '<textarea name="postscript" cols="80" rows="3" id="postscript" class="textarea"></textarea>';
//        $html .= '</td>';
//        $html .= '</tr>';
//        $html .= '</tbody>';
//        $html .= ' </table>';
//        $html .= '</div>';
//        $html .= '</td>';
//        $html .= '</tr>';
//        $html .= '</tbody>';
//        $html .= '</table>';
//        $html .= '<div class="blank10"></div>';

        //优惠券信息（平台优惠券）
        $a['voucher'] = getTddVoucher_PC($totalPrice);
        $a['html'] = $html;
        $a['mansong'] = $tddMS['mansong'];
        $a['totalPrice'] = $totalPrice;

        return $this->ajaxReturn($a);
    }

    //加载用户邮寄地址
    public function reloadAddress() {
        //获取用户的默认邮寄地址
        $where['user_id'] = session('user_id');
        $address = M('address')->where($where)->select();
        $html .= '<h3>确认收货地址 </h3>';
        $html .= '<div class="control"><div class="tc-btn createAddr theme-login am-btn am-btn-danger">使用新地址</div></div>';
        $html .= '<div class="clear"></div><ul>';
        foreach ($address as $k => $v) {
            $html .='<div class="per-border"></div>';
            if ($v['is_default'] == 1) {
                $html .= '<li onclick="Modifyaddress(' . $v[address_id] . ', $(this))" class="user-addresslist defaultAddr">';
            } else {
                $html .= '<li onclick="Modifyaddress(' . $v[address_id] . ', $(this))" class="user-addresslist">';
            }
            $html .= '<div class="address-left">';
            $html .= '<div class="user DefaultAddr">';
            $html .= '<span class="buy-address-detail"><span class="buy-user">' . $v[true_name] . ' </span><span class="buy-phone">' . $v[mob_phone] . '</span></span>';
            $html .= '</div><div class="default-address DefaultAddr">';
            $html .= '<span class="buy-line-title buy-line-title-type">收货地址：</span>';
            $html .= '<span class="buy--address-detail">'.$v['area_info'] . $v[address] . '</span></div>';
            $html .= '<ins class="deftip">默认地址</ins></div>';
            $html .= '<div class="address-right"><a href="../person/address.html"><span class="am-icon-angle-right am-icon-lg"></span></a></div>';
            $html .= '<div class="clear"></div>';
            $html .= '<div class="new-addr-btn">';
            $html .= '<a href="#" class="hidden">设为默认</a>';
            $html .= '<span class="new-addr-bar hidden">|</span>';
            $html .= '<a href="#">编辑</a>';
            $html .= '<span class="new-addr-bar">|</span>';
            $html .= '<a href="javascript:void(0);" onclick="delClick(this);">删除</a>';
            $html .= '</div></li>';
        }
        $html .= '</ul>';
        $html .= '<div class="clear"></div>';
        $html .= '</div>';
        return $this->ajaxReturn($html);
    }

    //选择默认地址
    public function setDefaultAddress() {
        $address_id = I('address_id');
        $map['address_id'] = $address_id;
        //将其它的地址设为0（非默认）
        $where['user_id'] = session('user_id');
        M('address')->where($where)->save(array('is_default' => 0));
        $a = M('address')->where($map)->save(array('is_default' => 1));
        $this->ajaxReturn($a);
    }

    //获取默认地址
    public function defaultAdderss() {
        //获取用户的默认邮寄地址
        $where['user_id'] = session('user_id');
        $where['is_default'] = "1";
        $v = M('address')->where($where)->find();

        $html .= '<div class="confirmAddr-addr" title="">';
        $html .= '<span class="confirmAddr-title">寄送至：</span>';
        $html .= '<span class="confirmAddr-addr-bd">';
        $html .= '<span class="prov"><br /> &nbsp;' . getCityName($v[province_id]) . '</span>';
        $html .= '<span class="city">' . getCityName($v[city_id]) . '</span>';
        $html .= '<span class="dist">' . getCityName($v[area_id]) . '</span>';
        $html .= '<span class="street">' . $v[address] . '</span>';
        $html .= '</span>';
        $html .= '</div>';
        $html .= '<div class="confirmAddr-addr-user">';
        $html .= '<span class="confirmAddr-title">收货人：</span>';
        $html .= '<span class="confirmAddr-addr-bd">';
        $html .= '<span>' . $v[true_name] . '</span>';
        $html .= '<span>' . $v[mob_phone] . '</span>';
        $html .= '</span>';
        return $this->ajaxReturn($html);
    }

    //添加地址
    public function address() {
        $ress_id = I('ress_id');
        $html = <<<EOD
                    <div class="mydiv-l" id="PopAddressTitle">使用新地址</div>
                        <div id="PopAddressCon">
                            <table cellpadding="4" cellspacing="4" width="100%" style="clear:both">
                                <tbody>
                                    <tr>
                                        <td style="text-align:right;width:13%"><font class="main-color">*</font>收件人</td>
                                        <td>
                                            <input type="text" name="consignee" value="" class="input_addr2">
                                            <div class="required-notice fl"><i class="notice-icon"></i><span class="notice-text reciever-notice"></span></div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="text-align:right"><font class="main-color">*</font>所在地区</td>
                                        <td>
                                        <div id="region">
                                            <input type="hidden" value="" name="city_id" id="city_id">
                                            <input type="hidden" name="area_id" id="area_id" value="" class="area_ids" />
                                            <input type="hidden" name="area_info" id="area_info" value="" class="area_names" />
                                            <select></select>
                                        </div>
                                            <div class="required-notice fl">
                                                <i class="notice-icon"></i><span class="notice-text address-notice"></span>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="text-align:right"><font class="main-color">*</font>街道地址</td>
                                        <td>
                                            <input type="text" name="address" class="input_addr1 inputMiddle" value="" />
                                            <div class="required-notice">
                                                <i class="notice-icon"></i><span class="notice-text detail-address-notice"></span>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="text-align:right">电子邮箱</td>
                                        <td>
                                            <input type="text" name="email" class="input_addr1 inputMiddle" value="" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="text-align:right">邮编</td>
                                        <td><input type="text" name="zipcode" class="input_addr3" value="" /></td>
                                    </tr>
                                    <tr>
                                        <td style="text-align:right"><font class="main-color">*</font>手机号码</td>
                                        <td>
                                            <input type="text" name="mobile" class="input_addr2 input_addr2_t" value="" /> &nbsp;&nbsp;&nbsp;&nbsp;或固定电话 
                                            <input type="text" name="tel_01" class="input_addr4" value="" /> - 
                                            <input type="text" name="tel_02" class="input_addr3" value="" /> - 
                                            <input type="text" name="tel_03" class="input_addr4" value="" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                        </td>
                                        <td>
                                            <font color="#cccccc" style="height:30px;line-height:30px;display:inline-block">两者至少填写一项</font>
                                            <div class="required-notice"><i class="notice-icon"></i><span class="notice-text phone-notice"></span></div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                        </td>
                                        <td style="padding-top:10px">
                                            <input type="button" class="BonusButton" value=" 确定 " onclick="submitAddress()" />
                                            &nbsp;&nbsp;<input type="button" class="Button_reset" value=" 取消 " onclick="closePopDiv()" />
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
	<!--F 省市区三级联动-->
	<script src="/Public/js/common_select.js"></script>
	<script type="text/javascript">
		regionInit("region");
	</script>
	<!--E 省市区三级联动-->
EOD;
        return $this->ajaxReturn($html);
    }

    //保存邮寄地址
    public function saveAddress() {
        //将其它的地址设为0（非默认）
        $where['user_id'] = session('user_id');
        M('address')->where($where)->save(array('is_default' => 0));

        $datas['user_id'] = session('user_id');
        $datas['true_name'] = I('consignee');
        $datas['area_info'] = I('area_info');
        $datas['city_id'] = I('city_id');
        $datas['area_id'] = I('area_id');
        $datas['address'] = I('address');
        $datas['tel_email'] = I('email');
        $datas['mob_phone'] = I('mobile');
        $datas['is_default'] = 1;
        $datas['zip_code'] = I('zipcode');
        $datas['landline'] = I('tel_01') . I('tel_02') . I('tel_03');
        $a = M('address')->add($datas);
        return $this->ajaxReturn($a);
    }

}